<template>
  <div class="mod-config">
    <el-form size="small" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.realName" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.username" placeholder="用户名" clearable></el-input>
      </el-form-item>
      <!--      <el-form-item>-->
      <!--        <el-input v-model="dataForm.username" placeholder="用户名" clearable></el-input>-->
      <!--      </el-form-item>-->

      <!--      <el-form-item>-->
      <!--        <span>老师类型:</span>-->
      <!--        <el-select style="width: 100px" v-model="dataForm.roleId" placeholder="请选择">-->
      <!--          <el-option-->
      <!--            v-for="item in options2"-->
      <!--            :key="item.value"-->
      <!--            :label="item.label"-->
      <!--            :value="item.value">-->
      <!--          </el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->

      <el-form-item>
        <span>学历:</span>
        <el-select style="width: 100px" v-model="dataForm.eduBg" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-radio-group v-model="dataForm.gender">
          <el-radio :lable="2">不限性别</el-radio>
          <el-radio :label="0">男</el-radio>
          <el-radio :label="1">女</el-radio>
        </el-radio-group>
        <!--        <el-radio v-model="dataForm.gender" :label="0">男</el-radio>-->
        <!--        <el-radio v-model="dataForm.gender" :label="1">女</el-radio>-->
      </el-form-item>
      <el-form-item>
        <!--        <el-button @click="getDataList()">查询</el-button>-->
        <!--        <el-button v-if="isAuth('modules:teacherinfo:save')" type="primary" @click="addOrUpdateHandle()">查询</el-button>-->
        <el-button v-if="isAuth('modules:teacherinfo:save')" type="primary" @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('modules:teacherinfo:delete')" type="warning" @click="agreeHandle()"
                   :disabled="dataListSelections.length <= 0">批量同意
        </el-button>
        <el-button v-if="isAuth('modules:teacherinfo:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量拒绝
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="教师的id">

      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="教师用户名">
      </el-table-column>
      <el-table-column
        prop="realName"
        header-align="center"
        align="center"
        label="真实姓名">
      </el-table-column>
      <el-table-column
        prop="gender"
        header-align="center"
        align="center"
        label="性别">
        <template slot-scope="scope">
          <div v-if="scope.row.gender === 0">男</div>
          <div v-if="scope.row.gender === 1">女</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="教师手机">
      </el-table-column>


      <!--      <el-table-column-->
      <!--        prop="photos"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="个人图片的地址">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="graduateSchool"
        header-align="center"
        align="center"
        label="毕业院校">
      </el-table-column>
      <el-table-column
        prop="eduBg"
        header-align="center"
        align="center"
        label="学历">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="roleId"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="老师类型">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-tag v-if="scope.row.roleId === 2">课程</el-tag>-->
      <!--          <el-tag v-if="scope.row.roleId === 5" style="background-color: #5cc0f5;color: white">直播</el-tag>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column-->
      <!--        prop="commistion"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="抽成余额">-->
      <!--      </el-table-column>-->

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope" >
          <el-button type="text" size="small"
                     @click="addOrUpdateHandle(scope.row)"
          >详情
          </el-button>
          <el-button style="background-color: #5cc0f5;color: #fffcfc" size="small" @click="agreeHandle(scope.row.id)">同意</el-button>
          <el-button size="small" style="background-color: #ff7474;color: #fffcfc" @click="deleteHandle(scope.row.id)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './teacherinfo-apply'

export default {
  data () {
    return {
      dataForm: {
        username: '',
        realName: '',
        gender: 2,
        eduBg: '',
        phone: '',
        roleId: '6',
      },
      options: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '博士后',
          label: '博士后'
        }, {
          value: '博士',
          label: '博士'
        }, {
          value: '硕士',
          label: '硕士'
        }, {
          value: '本科',
          label: '本科'
        }],
      options2: [
        {
          value: '',
          label: '全部老师'
        },
        {
          value: 5,
          label: '直播老师'
        }, {
          value: 2,
          label: '课程老师'
        },],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,

    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },

  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      let params = {
        'page': this.pageIndex,
        'limit': this.pageSize,
        'RoleId': this.dataForm.roleId,
        'realName': this.dataForm.realName,
        'username': this.dataForm.username,
        'phone': this.dataForm.phone,
        'eduBg': this.dataForm.eduBg,
        'gender': this.dataForm.gender
      }
      if (this.dataForm.gender === 2) {
        params.gender = ''
      }
      this.$http({
        url: this.$http.adornUrl('/teacher/teacherinfo/temp'),
        method: 'get',
        params: this.$http.adornParams(params)
      }).then(({data}) => {

        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          // console.log(this.dataList)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (teacher) {
      // dataList[scope.row.id]
      // console.log(id)

      this.addOrUpdateVisible = true
      //在dom元素渲染后在调用该函数 ---JOJO
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(teacher)
      })
    },
    //同意
    agreeHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]的老师进行[${id ? '同意' : '批量同意'}]成为老师操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/teacher/teacher/formal'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '同意成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]的老师进行[${id ? '拒绝' : '批量拒绝'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/teacher/teacher/refuse'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '拒绝成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
